using micromsg;
using MicroMsg.Common.Utils;
using MicroMsg.Protocol;
using System;
using System.Windows;
namespace MicroMsg.Network
{
	public class SessionPack
	{
		public const int STATUE_NOTSEND = 0;
		public const int STATUE_READY_SEND = 1;
		public const int STATUE_SENDING = 2;
		public const int STATUE_SENDED = 3;
		public const int STATUE_RECEIVED = 4;
		public const int STATUE_SUCCESSED = 5;
		public const int STATUE_SEND_ERR = 6;
		private static int gSeq = 1;
		public byte[] mResponseBuffer;
		public byte[] mCacheBodyBuffer;
		public bool mCanceled;
		public int mRetryLeft = 3;
		public double mSendTimeoutExpired;
		public double mPackTimeoutExpired = Util.getNowMilliseconds() + 100000.0;
		public int mSendStatus;
		private Connector.ConnectorContext mConnContext;
		public bool mNeedCompress = true;
		public short mEncrypt = 5;
		public object mRequestObject;
		public object mResponseObject;
		public object mUserArgs;
		private PackProgress mPackProgress = new PackProgress();
		public event SessionPackCompletedDelegate mCompleted;
		public event OnHttpReceivedProgressDelegate mHttpRecvProgress;
		public event RequestToByteArrayDelegate mProcRequestToByteArray;
		public event OnResponseParserDelegate mResponseParser;
		public int mConnectMode
		{
			get
			{
				return this.mConnContext.mConnectMode;
			}
			set
			{
				this.mConnContext.mConnectMode = value;
			}
		}
		public int mSeqID
		{
			get
			{
				return this.mConnContext.mSeq;
			}
			set
			{
				this.mConnContext.mSeq = value;
			}
		}
		public string mCmdUri
		{
			get
			{
				return this.mConnContext.mCmdUri;
			}
			set
			{
				this.mConnContext.mCmdUri = value;
			}
		}
		public int mCmdID
		{
			get
			{
				return this.mConnContext.mCmd;
			}
			set
			{
				this.mConnContext.mCmd = value;
				this.mConnContext.mCmdUri = CmdFunc.getUriByCmdID(value);
			}
		}
		public HttpClient mHttpClient
		{
			get
			{
				return this.mConnContext.mHttpClient;
			}
			set
			{
				if (value != null && this.mConnContext.mHttpClient != null)
				{
					this.mConnContext.mHttpClient.close();
				}
				this.mConnContext.mHttpClient = value;
				if (value != null)
				{
					this.mConnContext.mHttpClient.mHttpContext.mProgress += new OnHttpReceivedProgressDelegate(this.OnHttpReceivedProgress);
				}
			}
		}
		public static int getSeqID()
		{
			return SessionPack.gSeq++;
		}
		public void OnHttpReceivedProgress(int length)
		{
			if (this.mHttpRecvProgress != null)
			{
				this.mHttpRecvProgress(length);
			}
		}
		public SessionPack()
		{
			this.mConnContext = new Connector.ConnectorContext();
			this.mConnContext.mSeq = SessionPack.getSeqID();
		}
		public PackProgress getPackProgress()
		{
			this.mPackProgress.lengthSent = 0;
			this.mPackProgress.lengthReceived = 0;
			if (this.mCacheBodyBuffer != null)
			{
				this.mPackProgress.lengthSent = this.mCacheBodyBuffer.Length;
			}
			if (this.mResponseBuffer != null)
			{
				this.mPackProgress.lengthReceived = this.mResponseBuffer.Length;
			}
			else
			{
				if (this.mHttpClient != null)
				{
					this.mPackProgress.lengthReceived = this.mHttpClient.getReceivedLength();
				}
			}
			return this.mPackProgress;
		}
		public void printProgress()
		{
			this.getPackProgress();
		}
		public object onResponseParser()
		{
			if (this.mResponseParser != null)
			{
				return this.mResponseParser(this);
			}
			return null;
		}
		public static double getNow()
		{
			DateTime dateTime = new DateTime(1970, 1, 1);
			return DateTime.get_Now().Subtract(dateTime).get_TotalMilliseconds();
		}
		public void checkPackTimeout(double double_0)
		{
			if (double_0 >= this.mPackTimeoutExpired)
			{
				this.onError(PackResult.PACK_TIMEOUT);
				this.mSendStatus = 6;
			}
		}
		public bool isSendTimeout()
		{
			if (this.mSendTimeoutExpired == 0.0)
			{
				return false;
			}
			double now = SessionPack.getNow();
			return now >= this.mSendTimeoutExpired;
		}
		public void setTimeoutPoint()
		{
			double now = SessionPack.getNow();
			this.mSendTimeoutExpired = now + 30000.0;
		}
		public void cancelTimeoutPoint()
		{
			this.mSendTimeoutExpired = 0.0;
		}
		public int getMMFuncID()
		{
			return (int)CmdFunc.getMMFuncByCGI(this.mCmdUri);
		}
		private bool isAutoAuthPack()
		{
			if (this.mCmdID != 1)
			{
				return false;
			}
			AuthRequest authRequest = this.mRequestObject as AuthRequest;
			if (authRequest == null)
			{
				return false;
			}
			uint scene = authRequest.BaseRequest.Scene;
			return scene == 2u;
		}
		public void onError(PackResult packResult_0)
		{
			Log.smethod_1("Network", string.Concat(new object[]
			{
				"pack.onError, cmd = ",
				(CmdConst)this.mCmdID,
				" seq = ",
				this.mSeqID,
				", ret = ",
				packResult_0
			}));
			if (this.mCompleted != null)
			{
				PackEventArgs packEventArgs = new PackEventArgs();
				packEventArgs.result = packResult_0;
				if (this.isAutoAuthPack())
				{
					SessionPackMgr.setAuthStatus(0);
					return;
				}
				if (this.mCmdID == 1 || this.mCmdID == 32 || this.mCmdID == 31)
				{
					SessionPackMgr.setSessionKey(null);
					SessionPackMgr.getAccount().reset();
					SessionPackMgr.setAuthStatus(0);
				}
				Deployment.get_Current().get_Dispatcher().BeginInvoke(new SessionPackCompletedDelegate(this.mCompleted.Invoke), new object[]
				{
					this,
					packEventArgs
				});
			}
		}
		public void onSuccess()
		{
			Log.smethod_4("Network", string.Concat(new object[]
			{
				"pack.onSuccess, cmd = ",
				(CmdConst)this.mCmdID,
				" seq = ",
				this.mSeqID
			}));
			if (this.mCompleted != null)
			{
				PackEventArgs packEventArgs = new PackEventArgs();
				packEventArgs.result = PackResult.SUCCESS;
				Deployment.get_Current().get_Dispatcher().BeginInvoke(new SessionPackCompletedDelegate(this.mCompleted.Invoke), new object[]
				{
					this,
					packEventArgs
				});
			}
		}
		public byte[] requestToByteArray()
		{
			if (this.mRequestObject != null && this.mProcRequestToByteArray != null)
			{
				return this.mProcRequestToByteArray(this.mRequestObject);
			}
			return null;
		}
		public byte[] getSessionKey(bool encode)
		{
			int mCmdID = this.mCmdID;
			switch (mCmdID)
			{
			case 0:
				return this.getSessionKeyForInvalidCmd(encode);
			case 1:
				if (encode)
				{
					return null;
				}
				return Util.StringToByteArray(SessionPackMgr.getAccount().getPassword());
			default:
				switch (mCmdID)
				{
				case 31:
				case 32:
					if (encode)
					{
						return null;
					}
					return Util.StringToByteArray(SessionPackMgr.getAccount().getPassword());
				case 33:
					if (encode)
					{
						return null;
					}
					return Util.StringToByteArray(((GetUserNameRequest)this.mRequestObject).Pwd);
				default:
					if (mCmdID != 48)
					{
						return SessionPackMgr.getSessionKey();
					}
					if (encode)
					{
						return null;
					}
					return Util.StringToByteArray(((GetVerifyImgRequest)this.mRequestObject).Pwd.String);
				}
				break;
			}
		}
		private byte[] getSessionKeyForInvalidCmd(bool encode)
		{
			string mCmdUri;
			if ((mCmdUri = this.mCmdUri) != null)
			{
				if (!(mCmdUri == "/cgi-bin/micromsg-bin/getresetpwdurl"))
				{
					if (mCmdUri == "/cgi-bin/micromsg-bin/bindopmobileforreg")
					{
						if (encode)
						{
							return null;
						}
						return Util.StringToByteArray(((BindOpMobileRequest)this.mRequestObject).Mobile);
					}
				}
				else
				{
					if (encode)
					{
						return null;
					}
					return Util.StringToByteArray(((ResetPwdRequest)this.mRequestObject).Pwd);
				}
			}
			return SessionPackMgr.getSessionKey();
		}
	}
}
